﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Valor.DBUtility.MSSQLHelper;
using xTeam.HengLe.Entity;
using System.Data.SqlClient;
using System.Data;

namespace xTeam.HengLe.DataAccess
{
    public class NewsDataAccess
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectionString"></param>
        public NewsDataAccess(string connectionString)
        {
            SQLProcedureHelper.ConnectionString = connectionString;
        }
        /// <summary>
        /// 新闻的添加修改
        /// </summary>
        /// <param name="news">新闻实体</param>
        /// <returns>新闻编号</returns>
        public int Edit(ENews news)
        {
            int result = 0;
            SqlParameter[] parameters =
            {
                new SqlParameter("@newsid",SqlDbType.Int){Value = news.NewsId },
                new SqlParameter("@title",SqlDbType.NVarChar,500){Value = news.Title },
                new SqlParameter("@contents",SqlDbType.NText){Value = news.Contents },
                new SqlParameter("@author",SqlDbType.NVarChar,50){Value = news.Author },
                new SqlParameter("@source",SqlDbType.NVarChar,125){Value =news.Source },
                new SqlParameter("@isvalid",SqlDbType.Bit){Value = news.Isvalid },
                new SqlParameter("@keywords",SqlDbType.NVarChar,500){Value = news.Keywords },
                new SqlParameter("@Description",SqlDbType.NVarChar,500){Value =news.Description },
                new SqlParameter("@classifyid",SqlDbType.Int){Value = news.ClassifyId },
                new SqlParameter("@Sequence",SqlDbType.Int){Value = news.SortId },
                new SqlParameter("@AddDateTime",SqlDbType.DateTime){Value = news.WriteTime }
            };
            result = Convert.ToInt32(SQLProcedureHelper.ExecuteScalar("news_update", parameters));
            return result;
        }
        /// <summary>
        /// 删除新闻
        /// </summary>
        /// <param name="newsId">新闻编号</param>
        /// <returns>是否成功</returns>
        public int Delete(int newsId)
        {

            return SQLProcedureHelper.ExecuteNonQuery("news_delete"
                , new SqlParameter[] { 
                    new SqlParameter("@NewsId",SqlDbType.Int) {Value= newsId } 
                });

        }

        /// <summary>
        /// 条件查询新闻
        /// </summary>
        /// <param name="newsId">新闻编号(>0根据编号查询，=0查询所有，-1根据分类查询，其它查询)</param>
        /// <param name="key">标题</param>
        /// <param name="classifyid">分类</param>
        /// <returns>新闻列表</returns>
        public List<ENews> Search(string key, int classifyid, bool? isvalid)
        {
            List<ENews> newsList = new List<ENews>();
            SqlParameter[] parameters = 
            {
                new SqlParameter("@Key",SqlDbType.NVarChar,50){Value = key },
                new SqlParameter("@Classifyid",SqlDbType.Int){Value = classifyid},
                 new SqlParameter("@isvalid",SqlDbType.Bit){Value = isvalid}
            };

            SqlDataReader reader = SQLProcedureHelper.ExecuteReader("News_SelectList", parameters);

            while (reader.Read())
            {
                newsList.Add(new ENews()
                {
                    NewsId = Convert.ToInt32(reader["NewsId"]),
                    Title = Convert.ToString(reader["Title"]),
                    Contents = Convert.ToString(reader["Contents"]),
                    Author = Convert.ToString(reader["Author"]),
                    Source = Convert.ToString(reader["Source"]),
                    WriteTime = Convert.ToDateTime(reader["AddDateTime"]),
                    Hits = Convert.ToInt32(reader["ClickCount"]),
                    Isvalid = Convert.ToBoolean(reader["Isvalid"]),
                    Keywords = Convert.ToString(reader["Keywords"]),
                    Description = Convert.ToString(reader["Description"]),
                    ClassifyId = Convert.ToInt32(reader["ClassifyId"]),
                    SortId = Convert.ToInt32(reader["Sequence"])
                });
            }

            reader.Close();
            return newsList;
        }

        public ENews GetNewsById(int newsId, bool? isValid)
        {
            ENews news = null;
            SqlParameter[] parameters = 
            {
                new SqlParameter("@NewsId",SqlDbType.Int){Value = newsId},
                new SqlParameter("@IsValid",SqlDbType.Bit){Value = isValid}
            };
            SqlDataReader reader = SQLProcedureHelper.ExecuteReader("News_Select", parameters);
            if (reader.HasRows)
            {
                news = new ENews();
                reader.Read();

                news.NewsId = Convert.ToInt32(reader["NewsId"]);
                news.Title = Convert.ToString(reader["Title"]);
                news.Contents = Convert.ToString(reader["Contents"]);
                news.Author = Convert.ToString(reader["Author"]);
                news.Source = Convert.ToString(reader["Source"]);
                news.WriteTime = Convert.ToDateTime(reader["AddDateTime"]);
                news.Hits = Convert.ToInt32(reader["ClickCount"]);
                news.Isvalid = Convert.ToBoolean(reader["Isvalid"]);
                news.Keywords = Convert.ToString(reader["Keywords"]);
                news.Description = Convert.ToString(reader["Description"]);
                news.ClassifyId = Convert.ToInt32(reader["ClassifyId"]);
                news.SortId = Convert.ToInt32(reader["Sequence"]);
            }
            reader.Close();
            return news;

        }
    }
}
